﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using XuanFe.Database.Model;

namespace XuanFe.Database.DAL
{
    public class RoleModuleDAL
    {
        private const string SQL_SELECT_WS_ROLE_MODULE = "select ROM_ROLE_ID,ROM_MODULE_ID,ROM_RCRE_STAFF_ID,ROM_RCRE_TIME,ROM_LCHG_STAFF_ID,ROM_LCHG_TIME from WS_ROLE_MODULE where 1=1";
        private const string SQL_INSERT_WS_ROLE_MODULE = "insert into WS_ROLE_MODULE(ROM_ROLE_ID,ROM_MODULE_ID,ROM_RCRE_STAFF_ID,ROM_RCRE_TIME) values(@RoleId,@ModuleId,@LastStaffId,@LastTime)";
        private const string SQL_DELETE_WS_ROLE_MODULE = "delete from WS_ROLE_MODULE where 1=1";

        private const string SQL_WHERE_ROLE_ID = " and ROM_ROLE_ID=@RoleId ";

        private const string PARM_ROLE_ID = "@RoleId";
        private const string PARM_MODULE_ID = "@ModuleId";
        private const string PARM_LAST_STAFF_ID = "@LastStaffId";
        private const string PARM_LAST_TIME = "@LastTime";

        Nullable<DateTime> NullDateTime = null;
        private static RoleModuleDAL instance = new RoleModuleDAL();
        /// <summary>
        /// 初始化实例
        /// </summary>
        /// <returns></returns>
        public static RoleModuleDAL Instance
        {
            get
            {
                return instance;
            }
        }
        public List<RoleModuleModel> Get(string roleId)
        {
            StringBuilder sbSQL = new StringBuilder(SQL_SELECT_WS_ROLE_MODULE);
            sbSQL.Append(SQL_WHERE_ROLE_ID);

            List<MySqlParameter> parms = new List<MySqlParameter>();
            parms.Add(new MySqlParameter(PARM_ROLE_ID, MySqlDbType.String) { Direction = ParameterDirection.Input, Value = roleId });

            using (DataSet ds = MySQLHelper.Instance.ExecuteDataSet(MySQLHelper.Instance.ConnectionString, CommandType.Text, sbSQL.ToString(), parms.ToArray()))
            {
                DataTable dt = ds.Tables[0];
                List<RoleModuleModel> list = dt.AsEnumerable().Select(t => new RoleModuleModel
                {
                    RoleId = Convert.ToString(t.ItemArray[0]),
                    ModuleId = Convert.ToString(t.ItemArray[1]),
                    CreatedStaffId = Convert.ToString(t.ItemArray[2]),
                    CreatedDate = Convert.ToDateTime(t.ItemArray[3]),
                    LastModifiedStaffId = t.IsNull(4) ? string.Empty : Convert.ToString(t.ItemArray[4]),
                    LastModifiedDate = t.IsNull(5) ? NullDateTime : Convert.ToDateTime(t.ItemArray[5])
                }).ToList<RoleModuleModel>();

                return list;
            }
        }
    
        public int Insert(DbTransaction trans, RoleModuleModel model)
        {
            List<MySqlParameter> parms = new List<MySqlParameter>();
            parms.Add(new MySqlParameter(PARM_ROLE_ID, MySqlDbType.String) { Direction = ParameterDirection.Input, Value = model.RoleId });
            parms.Add(new MySqlParameter(PARM_MODULE_ID, MySqlDbType.String) { Direction = ParameterDirection.Input, Value = model.ModuleId });
            parms.Add(new MySqlParameter(PARM_LAST_STAFF_ID, MySqlDbType.String) { Direction = ParameterDirection.Input, Value = model.LastModifiedStaffId });
            parms.Add(new MySqlParameter(PARM_LAST_TIME, MySqlDbType.DateTime) { Direction = ParameterDirection.Input, Value = model.LastModifiedDate });

            return MySQLHelper.Instance.ExecuteNonQuery(trans, CommandType.Text, SQL_INSERT_WS_ROLE_MODULE, parms.ToArray());
        }
        public int Delete(DbTransaction trans, string roleId)
        {
            StringBuilder sbSQL = new StringBuilder(SQL_DELETE_WS_ROLE_MODULE);
            sbSQL.Append(SQL_WHERE_ROLE_ID);

            List<MySqlParameter> parms = new List<MySqlParameter>();
            parms.Add(new MySqlParameter(PARM_ROLE_ID, MySqlDbType.String) { Direction = ParameterDirection.Input, Value = roleId });

            return MySQLHelper.Instance.ExecuteNonQuery(trans, CommandType.Text, sbSQL.ToString(), parms.ToArray());
        }
    }
}
